package org.rulix.ogame.player.core;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.rulix.ogame.player.api.OgamePlanet;
import org.rulix.ogame.player.core.info.OgameDataHelper;

public class FindSpecificTags {
	
	
	
//	public static void main(String[] args) throws Exception {
//		String sourceUrlString="/Users/rulix/DatosRaul/Programming/eclipseIndigo/workspace/ogame-player-core/test_ogame.html";
//		Source source=new Source(new FileInputStream(sourceUrlString));
//
//
//		List<Element> smallplanets = source.getAllElementsByClass("smallplanet");
//
//
//		for (Element element : smallplanets) {
//			Element first = element.getFirstElement(HTMLElementName.A);
//
//			String valueHrefPlanet = first.getAttributeValue("href");
//			System.out.println("Contact URL: " + valueHrefPlanet);
//
//			List<NameValuePair> values = URLEncodedUtils.parse(new URI(valueHrefPlanet), null);
//			for (NameValuePair nameValuePair : values) {
//				System.out.println(nameValuePair);
//			}
//
//			first = element.getFirstElementByClass("planet-koords");
//			String koords = first.getContent().toString();
//			System.out.println("Coordinates: " +koords);
//
//			first = element.getFirstElementByClass("planet-name");
//			String planetName = first.getContent().toString();
//			System.out.println("PlanetName: " +planetName);
//		}
//	}

	
	public static void main(String[] args) throws Exception {
		OgameDataHelper odh = new OgameDataHelper();
		String sourceUrlString="/Users/rulix/DatosRaul/Programming/eclipseIndigo/workspace/ogame-player-core/test_ogame.html";
		StringBuilder sb = readInputStream(new FileInputStream(sourceUrlString));
		List<OgamePlanet> planets = odh.getPlanets(sb);
		
		for (OgamePlanet ogamePlanet : planets) {
			odh.fetchPlanetResources(ogamePlanet, sb);
			System.out.println(ogamePlanet.getPlanetInfo());
		}

		sourceUrlString="/Users/rulix/DatosRaul/Programming/eclipseIndigo/workspace/ogame-player-core/buildings.html";
		sb = readInputStream(new FileInputStream(sourceUrlString));
		
		for (OgamePlanet ogamePlanet : planets) {
			odh.fetchPlanetBuildings(ogamePlanet, sb);
			System.out.println(ogamePlanet.getPlanetInfo());
		}
//getFleetMovements

		sourceUrlString="/Users/rulix/DatosRaul/Programming/eclipseIndigo/workspace/ogame-player-core/eventList_Fleet.html";
		sb = readInputStream(new FileInputStream(sourceUrlString));
		
		odh.getFleetMovements(sb);		
	}
	
	public static StringBuilder readInputStream(InputStream is){
		
		int b=-1;
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String line = null;
		StringBuilder sb = new StringBuilder();
		try {
			while ( null != (line = br.readLine())){
				sb.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb;
	}

}